package com.example.fastjson;


import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import org.springframework.util.StopWatch;

import java.io.IOException;

public class Tester {

    private static final String str = "{\n" +
            "    \"name\": \"BeJson\",\n" +
            "    \"url\": \"http://www.bejson.com\",\n" +
            "    \"page\": 88,\n" +
            "    \"isNonProfit\": true,\n" +
            "    \"address\": {\n" +
            "        \"street\": \"科技园路.\",\n" +
            "        \"city\": \"江苏苏州\",\n" +
            "        \"country\": \"中国\"\n" +
            "    },\n" +
            "    \"links\": [\n" +
            "        {\n" +
            "            \"name\": \"Google\",\n" +
            "            \"url\": \"http://www.google.com\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"name\": \"Baidu\",\n" +
            "            \"url\": \"http://www.baidu.com\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"name\": \"SoSo\",\n" +
            "            \"url\": \"http://www.SoSo.com\"\n" +
            "        }\n" +
            "    ]\n" +
            "}";

    public static void main(String args[]) {
        JsonBean jsonBean = JSONObject.parseObject(str, new TypeReference<JsonBean>() {
        });
        try {
            byte[] bytes = ObjectAndByte.toByteArray(jsonBean);
            StopWatch stopWatch = new StopWatch();
            stopWatch.start("序列化");
            for (int i = 0; i < 10000; i++) {
                JsonBean jb = (JsonBean) ObjectAndByte.toObject(bytes);
            }
            stopWatch.stop();
            System.out.println("序列化耗时：" + stopWatch.getTotalTimeMillis());

            StopWatch sw = new StopWatch();
            sw.start("json");
            for (int i = 0; i < 10000; i++) {
                JSONObject jsonObject = JSONObject.parseObject(str);
                JsonBean jb = JSONObject.parseObject(str, new TypeReference<JsonBean>() {
                });
            }
            sw.stop();
            System.out.println("json 耗时 ： " + sw.getTotalTimeMillis());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
